package com.cop4331.social;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;

/**
 * 
 * @author Matt Szoke
 * @LastUpdated: 4/12/2012 by Matt Szoke
 * 
 * Functions to speak to communicate to Database
 * 
 */
public class UserFunctions {
	
	//IPs
	//final static String IP = "http://192.168.1.53/android/";
	final static String IP = "http://97.104.23.63/android/";

	private JSONParser jsonParser;

	private static String login_tag = "login";
	private static String register_tag = "register";
	private static String updatedetails_tag = "updateProfile";
	
	// -----------------------------------------------
	// CONSTRUCTOR
	// -----------------------------------------------
	public UserFunctions() {
		jsonParser = new JSONParser();
	}
	
	// -----------------------------------------------
	// RETRIEVE TUTOR NOTIFICATIONS
	// -----------------------------------------------
	public JSONObject retreiveTutorNotifications(String username, String type) {

		// Building Parameters
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", "tutorNot"));
		params.add(new BasicNameValuePair("un", username));
		params.add(new BasicNameValuePair("type", type));

		JSONObject json = jsonParser.getJSONFromUrl(IP, params);

		return json;

	}
	
	// -----------------------------------------------
	// SEND MAIL TO USER
	// -----------------------------------------------
	public void sendEmail(String full, String username, String password,
			String email) {

		// Building Paramters
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", "sendEmail"));
		params.add(new BasicNameValuePair("un", username));
		params.add(new BasicNameValuePair("password", password));
		params.add(new BasicNameValuePair("email", email));
		params.add(new BasicNameValuePair("full", full));

		JSONObject json = jsonParser.getJSONFromUrl(IP, params);

		return;
	}

	// -----------------------------------------------
	// RETRIEVE USER MESSAGES
	// -----------------------------------------------
	public JSONObject retreiveMessages(String username, String type) {

		// Building Parameters
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", "retreiveMessages"));
		params.add(new BasicNameValuePair("un", username));
		params.add(new BasicNameValuePair("type", type));

		JSONObject json = jsonParser.getJSONFromUrl(IP, params);

		return json;

	}
	
	// -----------------------------------------------
	// LOGIN USER
	// -----------------------------------------------
	public JSONObject loginUser(String email, String password) {
		// Building Parameters
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", login_tag));
		params.add(new BasicNameValuePair("email", email));
		params.add(new BasicNameValuePair("password", password));
		JSONObject json = jsonParser.getJSONFromUrl(IP, params);
		// return json
		// Log.e("JSON", json.toString());
		return json;
	}
	
	// -----------------------------------------------
	// UPDATE TUTOR
	// -----------------------------------------------
	public JSONObject updateTutor(String un, String cname, String term,
			String tutor) {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", "update_tutor"));
		params.add(new BasicNameValuePair("un", un));
		params.add(new BasicNameValuePair("cname", cname));
		params.add(new BasicNameValuePair("term", term));
		params.add(new BasicNameValuePair("tutor", tutor));

		JSONObject json = jsonParser.getJSONFromUrl(IP, params);
		// return json
		// Log.e("JSON", json.toString());
		return json;
	}
	
	// -----------------------------------------------
	// UPDATE CLASS
	// -----------------------------------------------
	public static void updateClass(String id, String tutor, String grade) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", "updateClass"));
		params.add(new BasicNameValuePair("grade", grade));
		params.add(new BasicNameValuePair("id", id));
		params.add(new BasicNameValuePair("tutor", tutor));

		JSONParser jParser = new JSONParser();

		JSONObject json = jParser.getJSONFromUrl(IP, params);
		// return json
		// Log.e("JSON", json.toString());
		return;
	}

	// -----------------------------------------------
	// GET IP
	// -----------------------------------------------
	public static String getIp() {
		return IP;
	}
	
	// -----------------------------------------------
	// ADD SESSION
	// -----------------------------------------------
	public JSONObject addSession(String tutor, String tutoree, String date,
			String time, String location, String subject, String other,
			String status) {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", "addsession"));
		params.add(new BasicNameValuePair("tutor", tutor));
		params.add(new BasicNameValuePair("tutoree", tutoree));
		params.add(new BasicNameValuePair("date", date));
		params.add(new BasicNameValuePair("time", time));
		params.add(new BasicNameValuePair("location", location));
		params.add(new BasicNameValuePair("subject", subject));
		params.add(new BasicNameValuePair("other", other));
		params.add(new BasicNameValuePair("status", status));

		JSONObject json = jsonParser.getJSONFromUrl(IP, params);

		return json;
	}
	
	// -----------------------------------------------
	// RETRIEVE ALL OF USER COURSES
	// -----------------------------------------------
	public JSONObject retrieveAllCourses(String un) {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", "returnAllCourses"));
		params.add(new BasicNameValuePair("un", un));

		JSONObject json = jsonParser.getJSONFromUrl(IP, params);
		return json;
	}
	
	// -----------------------------------------------
	// RETRIEVE LIST OF AVAILABLE TUTORS
	// -----------------------------------------------
	public JSONObject retrieveTutors(String course, String term) {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", "retrieveAvailableTutors"));
		params.add(new BasicNameValuePair("cname", course));
		params.add(new BasicNameValuePair("term", term));

		JSONObject json = jsonParser.getJSONFromUrl(IP, params);
		return json;
	}
	
	// -----------------------------------------------
	// UPDATE IMAGE
	// -----------------------------------------------
	public JSONObject updateImg(String un) {

		System.out.println(un);

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", "updateImg"));
		params.add(new BasicNameValuePair("userName", un));

		JSONObject json = jsonParser.getJSONFromUrl(IP, params);

		return json;
	}
	
	// -----------------------------------------------
	// EDIT PROFILE INFO
	// -----------------------------------------------
	public JSONObject editUserDetails(String un, String full, String email,
			String phone, String password, String gender, String sec,
			String ans, String maj) {
		// Building Parameters
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", updatedetails_tag));
		params.add(new BasicNameValuePair("userName", un));
		params.add(new BasicNameValuePair("full", full));
		params.add(new BasicNameValuePair("phone", phone));
		params.add(new BasicNameValuePair("email", email));
		params.add(new BasicNameValuePair("password", password));
		params.add(new BasicNameValuePair("gender", gender));
		params.add(new BasicNameValuePair("security", sec));
		params.add(new BasicNameValuePair("ans", ans));
		params.add(new BasicNameValuePair("major", maj));

		// getting JSON Object
		JSONObject json = jsonParser.getJSONFromUrl(IP, params);
		// return json
		return json;
	}
	
	// -----------------------------------------------
	// REGISTER USER
	// -----------------------------------------------
	public JSONObject registerUser(String un, String full, String email,
			String phone, String password, String img, String gender,
			String sec, String ans, String maj) {
		// Building Parameters
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", register_tag));
		params.add(new BasicNameValuePair("userName", un));
		params.add(new BasicNameValuePair("full", full));
		params.add(new BasicNameValuePair("phone", phone));
		params.add(new BasicNameValuePair("email", email));
		params.add(new BasicNameValuePair("password", password));
		params.add(new BasicNameValuePair("img", img));
		params.add(new BasicNameValuePair("gender", gender));
		params.add(new BasicNameValuePair("security", sec));
		params.add(new BasicNameValuePair("ans", ans));
		params.add(new BasicNameValuePair("major", maj));

		// getting JSON Object
		JSONObject json = jsonParser.getJSONFromUrl(IP, params);
		// return json
		return json;
	}

	// -----------------------------------------------
	// ADD CLASS TO DATABASE
	// -----------------------------------------------
	// To reference later see, retrieveAllCourses method
	public JSONObject addClass(String course, String un, String term,
			String cgrade) {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", "addcourse_tag"));
		params.add(new BasicNameValuePair("course", course));
		params.add(new BasicNameValuePair("username", un));
		params.add(new BasicNameValuePair("term", term));
		params.add(new BasicNameValuePair("grade", cgrade));

		JSONObject json = jsonParser.getJSONFromUrl(IP, params);

		return json;

	}
	
	// -----------------------------------------------
	// CHECK IF USEREXISTS
	// -----------------------------------------------
	public JSONObject userExists(String un) {

		List<NameValuePair> params = new ArrayList<NameValuePair>();

		params.add(new BasicNameValuePair("tag", "userExists"));
		params.add(new BasicNameValuePair("un", un));

		JSONObject json = jsonParser.getJSONFromUrl(IP, params);

		return json;

	}

	/**
	 * Function get Login status
	 * */
	public boolean isUserLoggedIn(Context context) {
		DatabaseHandler db = new DatabaseHandler(context);
		int count = db.getRowCount();
		if (count > 0) {
			// user logged in
			return true;
		}
		return false;
	}

	/**
	 * Function to logout user Reset Database
	 * */
	public boolean logoutUser(Context context) {
		DatabaseHandler db = new DatabaseHandler(context);
		db.resetTables();
		return true;
	}

	// -----------------------------------------------
	// UPDATE SESSION INFO
	// -----------------------------------------------
	public void updateSession(String id, String value) {

		List<NameValuePair> params = new ArrayList<NameValuePair>();

		params.add(new BasicNameValuePair("tag", "updateSession"));
		params.add(new BasicNameValuePair("id", id));
		params.add(new BasicNameValuePair("v", value));

		JSONObject json = jsonParser.getJSONFromUrl(IP, params);
	}
	
	// -----------------------------------------------
	// UPDATE MESSAGE
	// -----------------------------------------------
	public void updateMessage(String id) {

		List<NameValuePair> params = new ArrayList<NameValuePair>();

		params.add(new BasicNameValuePair("tag", "updateMessage"));
		params.add(new BasicNameValuePair("id", id));

		JSONObject json = jsonParser.getJSONFromUrl(IP, params);
	}
	
	// -----------------------------------------------
	// UPDATE SESSION INFORMATION
	// -----------------------------------------------
	public void updateSessionInfo(String id, String tutor, String tutoree,
			String date, String time, String location, String subject,
			String other, String status) {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", "updateSessionInfo"));
		params.add(new BasicNameValuePair("id", id));
		params.add(new BasicNameValuePair("tutor", tutor));
		params.add(new BasicNameValuePair("tutoree", tutoree));
		params.add(new BasicNameValuePair("date", date));
		params.add(new BasicNameValuePair("time", time));
		params.add(new BasicNameValuePair("location", location));
		params.add(new BasicNameValuePair("subject", subject));
		params.add(new BasicNameValuePair("other", other));
		params.add(new BasicNameValuePair("status", status));

		JSONObject json = jsonParser.getJSONFromUrl(IP, params);

	}

	// -----------------------------------------------
	// DELETE MESSAGE
	// -----------------------------------------------
	public void deleteMessage(String id) {

		List<NameValuePair> params = new ArrayList<NameValuePair>();

		params.add(new BasicNameValuePair("tag", "deleteMessage"));
		params.add(new BasicNameValuePair("id", id));

		JSONObject json = jsonParser.getJSONFromUrl(IP, params);
	}
	
	// -----------------------------------------------
	// DELETE SESSION
	// -----------------------------------------------
	public void deleteSession(String id) {

		List<NameValuePair> params = new ArrayList<NameValuePair>();

		params.add(new BasicNameValuePair("tag", "deleteSession"));
		params.add(new BasicNameValuePair("id", id));

		JSONObject json = jsonParser.getJSONFromUrl(IP, params);
	}

	// -----------------------------------------------
	// UPDATE USER RATING
	// -----------------------------------------------
	public void updateRating(String un, String rating, String count) {

		List<NameValuePair> params = new ArrayList<NameValuePair>();

		params.add(new BasicNameValuePair("tag", "updateRating"));
		params.add(new BasicNameValuePair("un", un));
		params.add(new BasicNameValuePair("rating", rating));
		params.add(new BasicNameValuePair("count", count));

		JSONObject json = jsonParser.getJSONFromUrl(IP, params);
	}

	// -----------------------------------------------
	// ADD A MESSAGE TO DATABASE
	// -----------------------------------------------
	public void addMessage(String un, String username, String subIn, String mess) {

		List<NameValuePair> params = new ArrayList<NameValuePair>();

		params.add(new BasicNameValuePair("tag", "addMessage"));
		params.add(new BasicNameValuePair("from", un));
		params.add(new BasicNameValuePair("to", username));
		params.add(new BasicNameValuePair("sub", subIn));
		params.add(new BasicNameValuePair("mess", mess));

		JSONObject json = jsonParser.getJSONFromUrl(IP, params);

	}

	// -----------------------------------------------
	// CHECK IF STRING IS VALID EMAIL
	// -----------------------------------------------
	public static boolean validEmail(String s) {

		String email = s;
		Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
		Matcher m = p.matcher(email);
		boolean matchFound = m.matches();

		if (matchFound)
			return true;
		else
			return false;

	}

	// -----------------------------------------------
	// CHECK IF USERNAME IS VALID
	// -----------------------------------------------
	public static boolean stringValid(String s) {

		Pattern p = Pattern.compile("^[A-Za-z0-9]+$");

		return p.matcher(s).matches();
	}

}
